A new high-contrast imaging subtraction algorithm (TLOCI) is presented to maximize a planet signal-to-noise ratio. The technique uses an input spectrum and template PSFs to optimize the reference image coefficient determination to minimize the flux contamination via self-subtraction (thus maximizing its throughput wavelength per wavelength) of any planet that have a similar spectrum to the template spectrum in the image, while trying, at the same time, to maximize the speckle noise subtraction. The optimization is performed by a correlation matrix conditioning. Using laboratory Gemini Planet Imager data, the new algorithm is shown to be superior to the simple/double difference, polynomial fit and original LOCI algorithm.